<template>
  <el-dialog title="员工上传" width="500px" :visible="showExcelDialog" @close="hClose">
    <el-row type="flex" justify="center">
      <div class="upload-excel">
        <input
          ref="excel-upload-input"
          class="excel-upload-input"
          type="file"
          accept=".xlsx"
          @change="uploadChange"
        >
        <div class="drop" @dragover="handleDragover" @drop="handleDrop">
          <i class="el-icon-upload" />
          <el-button type="text" @click="getTemplate">下载导入模板</el-button>
          <span>将文件拖到此处或
            <el-button type="text" @click="hUpload">点击上传</el-button>
          </span>
        </div>
      </div>
    </el-row>
    <el-row type="flex" justify="end">
      <!-- update:props属性名，值 直接修改 .sync修饰符的属性值 -->
      <el-button size="mini" type="primary" @click="$emit('update:showExcelDialog', false)">取消</el-button>
    </el-row>
  </el-dialog>
</template>

<script>
import { getExportTemplate, uploadExcel } from '@/api/employee'
import FileSaver from 'file-saver'
export default {
  props: {
    showExcelDialog: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    // 关闭弹框
    hClose() {
      this.$emit('update:show-excel-dialog', false)
    },
    // 下载模板
    async getTemplate() {
    //   console.log('下载模板')
      const res = await getExportTemplate()
      //   console.log(res)
      // 保存文件
      FileSaver.saveAs(res, '下载导入模板')
    },
    // 上传文件
    hUpload() {
      // this.$refs.属性名 和 this.$refs[属性名] 等价
      this.$refs['excel-upload-input'].click()
    },
    // 上传文件 是change input事件
    async uploadChange(e) {
    //   console.log(e)// target.files
    //   console.log(e.target.files)
      const file = e.target.files[0]
      //   console.log(file)
      if (file) {
        // 1.准备参数文件上传的参数 -固定格式
        const fs = new FormData()
        fs.append('file', file)
        try {
          // 2.调用接口,做文件上传
          await uploadExcel(fs)
          this.$message.success('操作成功')
          // 3.通知父组件更新数据
          // 关闭弹框
          this.$emit('update:show-excel-dialog', false)
        } finally {
          // 不论成功或者失败都会执行finally
          // 重置input的value,让它下次选中同一个文件时,也能触发change
          this.$refs['excel-upload-input'].value = ''
        }
      }
    },
    // @dragover
    handleDragover(e) {
      e.preventDefault() // 阻止默认行为
    },
    // @drop
    async handleDrop(e) {
      e.preventDefault()
      const file = e.dataTransfer.files[0]
      console.log(file) // 打印选中的文件信息
      // 在这里可以将文件信息传递给后端进行处理
      this.doUploadFile(file)
    }

  }
}
</script>
<style scoped lang="scss">
    .upload-excel {
      display: flex;
      justify-content: center;
      margin: 20px;
      width: 360px;
      height: 180px;
      align-items: center;
      color: #697086;
      .excel-upload-input {
        display: none;
        z-index: -9999;
      }
      .btn-upload,
      .drop {
        border: 1px dashed #dcdfe6;
        width: 100%;
        height: 100%;
        text-align: center;
        line-height: 160px;
        border-radius: 8px;
        display: flex;
        flex-direction: column;
        justify-content: center;
      }
      .drop {
        line-height: 40px;
        color: #bbb;
        i {
          font-size: 60px;
          display: block;
          color: #c0c4cc;
        }
      }
    }
</style>
